//
//  ChangeEmailViewController.swift
//  AESOLAR_IOS
//
//  Created by Mr.Wang on 2019/8/29.
//  Copyright © 2019 Mr.Wang. All rights reserved.
//

import UIKit

class ChangeEmailViewController: BaseUIViewController {

    let email: String
    
    let textField = UITextField()
    
    init(email: String) {
        self.email = email
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = #colorLiteral(red: 0.9724436402, green: 0.972609818, blue: 0.9724331498, alpha: 1)
        self.navigationItem.title = "请输入新邮箱".localized
        
        let text = String.init(format: "你的帐号目前绑定邮箱是 %@，请输入你希望绑定的新邮箱。".localized, email)
        let attributeString = NSMutableAttributedString.init(string: text)
        attributeString.setParagraphSpace(8)
        
        let tipLabel = UILabel().config
            .attributedText(attributeString)
            .font(.systemFont(ofSize: 14, weight: .medium))
            .textColor(#colorLiteral(red: 0.2, green: 0.2, blue: 0.2, alpha: 1))
            .numberOfLines(0)
            .addTo(view)
            .makeConstraints { (make) in
                make.left.top.equalTo(20)
                make.centerX.equalToSuperview()
        }.view
        
        let contentView = UIView().config
            .backgroundColor(.white)
            .addTo(self.view)
            .makeConstraints { (make) in
                make.left.right.equalToSuperview()
                make.height.equalTo(60)
                make.top.equalTo(tipLabel.snp.bottom).offset(15)
        }.view
        
        textField.config
            .font(.systemFont(ofSize: 14, weight: .medium))
            .textColor(#colorLiteral(red: 0.2, green: 0.2, blue: 0.2, alpha: 1))
            .placeholder("请输入邮箱".localized)
            .addTo(contentView)
            .makeConstraints { (make) in
                make.center.equalToSuperview()
                make.height.equalTo(40)
                make.left.equalTo(20)
        }
        
        UIButton().config
            .backgroundImage(UIImage.imageWithColor(APPConfig.baseColor))
            .title("发送验证码".localized)
            .font(.systemFont(ofSize: 19, weight: .medium))
            .titleColor(.white)
            .cornerRadius(5)
            .masksToBounds(true)
            .addTarget(self, action: #selector(sendCode))
            .addTo(view)
            .makeConstraints { (make) in
                make.left.equalTo(30)
                make.top.equalTo(contentView.snp.bottom).offset(30)
                make.height.equalTo(50)
                make.centerX.equalToSuperview()
        }
    }
    
    @objc func sendCode() {
        guard let email = self.textField.text,
            email.count > 0 else {
                APPHud.shared.showError(status: "请输入邮箱".localized)
                return
        }
        
        if email == self.email {
            APPHud.shared.showError(status: "新邮箱不能和当前邮箱相同".localized)
            return
        }
        
        guard email.isEmail else {
            APPHud.shared.showError(status: "邮箱格式不正确".localized)
            return
        }
        
        let vc = SendEmailCodeViewController.init(email: email)
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        textField.becomeFirstResponder()
    }
    
    override func navigationBarStyle() -> NavigationBarStyle {
        return .baseColor
    }
    
}
